const { TreeNode, BinaryTreeTraversal } = require("../template")

function testBinaryTreeDepthBalance() {
  const traversal = new BinaryTreeTraversal()

  console.log(
    "1. 最大深度测试:",
    (() => {
      // 构建测试树:    1
      //              /   \
      //             2     3
      //            / \   / \
      //           4   5 6   7
      //          /
      //         8
      const deepTree = new TreeNode(1)
      deepTree.left = new TreeNode(2)
      deepTree.right = new TreeNode(3)
      deepTree.left.left = new TreeNode(4)
      deepTree.left.right = new TreeNode(5)
      deepTree.right.left = new TreeNode(6)
      deepTree.right.right = new TreeNode(7)
      deepTree.left.left.left = new TreeNode(8)
      
      return traversal.maxDepth(deepTree) === 4 &&
             traversal.maxDepth(new TreeNode(1)) === 1 &&
             traversal.maxDepth(null) === 0
    })()
  )

  console.log(
    "2. 平衡二叉树测试:",
    (() => {
      // 构建平衡树:    1
      //              /   \
      //             2     3
      //            / \   / \
      //           4   5 6   7
      const balancedTree = new TreeNode(1)
      balancedTree.left = new TreeNode(2)
      balancedTree.right = new TreeNode(3)
      balancedTree.left.left = new TreeNode(4)
      balancedTree.left.right = new TreeNode(5)
      balancedTree.right.left = new TreeNode(6)
      balancedTree.right.right = new TreeNode(7)
      
      return traversal.isBalanced(balancedTree) === true
    })()
  )

  console.log(
    "3. 不平衡树测试:",
    (() => {
      // 构建不平衡树: 1
      //              /
      //             2
      //            /
      //           3
      //          /
      //         4
      const unbalancedTree = new TreeNode(1)
      unbalancedTree.left = new TreeNode(2)
      unbalancedTree.left.left = new TreeNode(3)
      unbalancedTree.left.left.left = new TreeNode(4)
      
      return traversal.isBalanced(unbalancedTree) === false
    })()
  )

  console.log(
    "4. 部分不平衡树测试:",
    (() => {
      // 构建部分不平衡树: 1
      //                  /   \
      //                 2     3
      //                / \   /
      //               4   5 6
      //              /
      //             7
      const partiallyUnbalanced = new TreeNode(1)
      partiallyUnbalanced.left = new TreeNode(2)
      partiallyUnbalanced.right = new TreeNode(3)
      partiallyUnbalanced.left.left = new TreeNode(4)
      partiallyUnbalanced.left.right = new TreeNode(5)
      partiallyUnbalanced.right.left = new TreeNode(6)
      partiallyUnbalanced.left.left.left = new TreeNode(7)
      
      return traversal.isBalanced(partiallyUnbalanced) === false
    })()
  )

  console.log(
    "5. 单节点平衡测试:",
    (() => {
      const singleNode = new TreeNode(42)
      return traversal.isBalanced(singleNode) === true &&
             traversal.maxDepth(singleNode) === 1
    })()
  )

  console.log(
    "6. 空树平衡测试:",
    (() => {
      return traversal.isBalanced(null) === true &&
             traversal.maxDepth(null) === 0
    })()
  )

  console.log(
    "7. this上下文深度平衡测试:",
    (() => {
      const depthObj = {
        multiplier: 2,
        traversal: new BinaryTreeTraversal(),
        processDepth: function(root) {
          const depth = this.traversal.maxDepth(root)
          return depth * this.multiplier
        }
      }
      const testTree = new TreeNode(1)
      testTree.left = new TreeNode(2)
      testTree.right = new TreeNode(3)
      return depthObj.processDepth(testTree) === 4 // 2 * 2 = 4
    })()
  )

  console.log(
    "8. 复杂对象this绑定深度平衡测试:",
    (() => {
      const depthConfigObj = {
        config: { offset: 3 },
        traversal: new BinaryTreeTraversal(),
        processWithConfig: function(root) {
          const isBalanced = this.traversal.isBalanced(root)
          return isBalanced ? this.config.offset : 0
        }
      }
      const testTree = new TreeNode(1)
      testTree.left = new TreeNode(2)
      testTree.right = new TreeNode(3)
      return depthConfigObj.processWithConfig(testTree) === 3 // 平衡树返回offset
    })()
  )
}

testBinaryTreeDepthBalance()
